package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.admin.common.exception.RunException;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.message.service.impl.O2oMessageServiceImpl;
import com.eascs.pay.entity.O2oPayEntity;
import com.eascs.pay.service.impl.O2oPayServiceImpl;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 内审流程
 */
@Service
public class NSMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(NSMsgNotifyServiceImpl.class);

	
	@Autowired
	private O2oMessageServiceImpl o2oMessageServiceImpl;
	
	@Autowired
	private O2oPayServiceImpl o2oPayServiceImpl;
	
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	
	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply){
		ModelResult<O2oLoanapplyEntity> modelResult =new ModelResult<>();
		log.info("启动内审流程通知:[{}]", StatusContexts.WFTYPE_NS);
		log.info("内审流程消息通知:[{}]", notifyParam.getProcessType());
		
		loanapply = nsFlow(notifyParam, loanapply);
	/*	if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {
			log.info("归档流程通过记录通知：{}", loanapply.getBillCode());
			loanapply.setIsPlaceOnFile(StatusContexts.WFTYPE_STATUS_CLO);
			loanapply.setPlaceOnFileTime(new Date());
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {
			log.info("归档流程驳回记录通知：{}", loanapply.getBillCode());
			loanapply.setIsPlaceOnFile(StatusContexts.O2O_NEW);
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {
			log.info("归档流程作废记录通知：{}", loanapply.getBillCode());
		}*/
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add(ProcessTemplate.PROECESS_TYPE_INAUDIT);
		list.add(ProcessTemplate.PROECESS_TYPE_INAUDIT_FQ);
		list.add(ProcessTemplate.PROECESS_TYPE_INAUDIT_BP);
		return list;
	}
	

	@Override
	public Boolean checkReturn() {
		return Boolean.FALSE;
	}
	
	/**
	 * 内审流程
	 * 
	 * @param notifyParam
	 * @param loanapplyEntity
	 * @return
	 */
	private O2oLoanapplyEntity nsFlow(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapplyEntity) {
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {// 流程通过
			log.info("贷款编号:[{}],内审流程完成,修改为主表状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.O2O_ZX_END);
			loanapplyEntity.setWfType(StatusContexts.O2O_NS_END);
			try {
				ModelResult<Boolean> messageResult = o2oMessageServiceImpl.nsEndMessage(loanapplyEntity.getBillCode());
				if (messageResult.getModel()) { // 如果是返回的是true，说明是成功的
					log.info("billCode:[{}],内审完成给经办人,客户发送短信成功", loanapplyEntity.getBillCode());
				} else {
					log.error("billCode:[{}],内审完成给经办人,客户发送短信失败", loanapplyEntity.getBillCode());
				}
			} catch (Exception e) {
				log.error("billCode:[{}],内审完成发送短信失败：", loanapplyEntity.getBillCode());
			}
			if (loanapplyEntity.getLoanType().equals(StatusContexts.LOAN_TYPE_EA) || loanapplyEntity.getLoanType().equals(StatusContexts.LOAN_TYPE_FQ)) {
				try {
					loanapplyEntity.setRepaymentSource(StatusContexts.DATA_SOURCE_NEW);
					int i = o2oPayServiceImpl.insertPayByEntity(loanapplyEntity, StatusContexts.PAY_TYPE_ZD, null);
					if (i > 0) {
						log.info("内审完成插入付款流程数据成功，贷款单号为：{}", notifyParam.getReferId());
						O2oLoanUserEntity loanUserEntity = o2oLoanapplyService.findLoanUserEntity(loanapplyEntity.getLoanapplyNo(), loanapplyEntity.getLoanId());
						ModelResult<Boolean> model = o2oPayServiceImpl.startPayFlow(loanapplyEntity, loanUserEntity.getUserFullName());
						if (model.isSuccess()) {
							log.info("内审完成启动付款流程数据成功，贷款单号为：{}", notifyParam.getReferId());
							O2oPayEntity o2oPayEntity = o2oPayServiceImpl.findEntityByLoanapplyId(loanapplyEntity.getLoanapplyNo());
							if (null != o2oPayEntity) {
								o2oPayEntity.setSubmitTime(new Date());
								o2oPayEntity.setSubmitUser(loanapplyEntity.getOperatorNo());
								o2oPayEntity.setBillStatus(StatusContexts.O2O_BANK_RUN);
								o2oPayEntity.setLastUpdateTime(new Date());
								int flag = o2oPayServiceImpl.updateEntityById(o2oPayEntity);
								if (flag <= 0) {
									log.info("内审完成启动付款流程数据成功，贷款单号为：{},更新付款数据失败", notifyParam.getReferId());
									throw new RunException("贷款单号：" + loanapplyEntity.getBillCode() + "分期贷和接力贷自动启动付款流程,更新付款数据失败");
								}
								log.info("内审完成启动付款流程数据成功，贷款单号为：{},更新付款数据成功", notifyParam.getReferId());
							}
						} else {
							log.error("内审完成启动付款流程数据成功，贷款单号为：{},失败原因：{}", notifyParam.getReferId(), model.getDetailStack());
						}
					}
				} catch (Exception e) {
					log.error("内审完成插入启动付款流程数据失败,贷款单号为：{},失败原因：{}", notifyParam.getReferId(), e.getMessage());
				}
			}
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {// 流程驳回
			log.info("贷款编号:[{}],内审流程驳回,修改为主表状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.O2O_ZX_END);
			loanapplyEntity.setWfType(StatusContexts.O2O_ZX_END);
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {// 作废
			log.info("贷款编号:[{}],内审流程作废,修改为主表状态为：[{}]", loanapplyEntity.getBillCode(), StatusContexts.O2O_FK_OVER);
			loanapplyEntity.setWfType(StatusContexts.O2O_FK_OVER);
		}
		return loanapplyEntity;
	}

	
}
